<?php
class Training_CaptchaController extends Zendvn_Controller_Action{
	
	private $_arrParam;
	
	public function init(){
		parent::init();
		$this->_arrParam = $this->_request->getParams();
	}
	
	public function indexAction(){
		
		$arrWord = array("0","1","2","3","4","5","6","7","8","9");
		
		$captcha = new Zend_Captcha_Image();
		$captcha->setImgDir(CAPTCHA_PATH . '/img');
		$captcha->setImgUrl(CAPTCHA_URL . '/img');
		$captcha->setImgAlt('Security code');
		$captcha->setFont(CAPTCHA_PATH . '/font/abc.ttf');
		$captcha->setWordlen(4);
		$captcha->setFontSize(42);
		$captcha->setWidth(300);
		$captcha->setHeight(100);
		$captcha->setTimeout(100);
		$captcha->setUseNumbers(false);
		$captcha->setStartImage(CAPTCHA_PATH . '/bg/captcha_background.png');
		//$captcha->set
		$captcha->generate();
		
		echo '<br>' . $this->view->captchaID = $captcha->getId();
		echo '<pre>';
		print_r($captcha);
		echo '</pre>';
		$ssCaptcha = $captcha->getSession();
		echo '<br>' . $ssCaptcha->word = $captcha->getWord();
		
		$this->view->captcha = $captcha->render($this->view);
		
		if($this->_request->isPost()){
			
			$namespace = 	'Zend_Form_Captcha_' . $this->_arrParam['captchaID'];
			$ssCode = new Zend_Session_Namespace($namespace);			
			
			if($this->_arrParam['code'] === $ssCode->word){
				echo '<br>' . 'Gia tri nhap dung';
			}else{
				echo '<br>' . 'Nhap gia tri sai';
			}
			$upload = new Zendvn_File_Upload();
			$fileName = $this->_arrParam['captchaID'] . $captcha->getSuffix();
			$upload->remove($fileName,CAPTCHA_PATH . '/img');
		}
		/*echo '<pre>';
		print_r($captcha);
		echo '</pre>';*/
		
		//$this->_helper->viewRenderer->setNoRender();
	}
	
}